
const LineLossTempReducer = (prevState = {
  luckySheetOption: "",
  // {
  //   container: "luckysheet",
  //   lang: 'zh',
  //   showinfobar: false,
  // }
  tempList: [],
  viewId: 0,
  stateId: 0,
  insertDate: 'noDate',
  saveCommit: "",  //保存时获取到用户输入的备注

  //  ...
}, action) => {
  let newState = { ...prevState }
  switch (action.type) {
    // 每次查询的时候，都将id设为viewId
    case "change-luckySheetConfStr":
      let defaultOption = {
        container: "luckysheet",
        lang: 'zh',
        showinfobar: false,
      }
      //let newOption = JSON.stringify(defaultOption)

      // 如果是新模板过来，就是配置为空，data为空
      if (action.payload.luckySheetConfigStr === "" || action.payload.luckySheetConfigStr === undefined) {

        // 如果是正常的模板过来，就显示对应的模板
      } else {
        //这是从后端获取的数据，直接赋值给data
        defaultOption.data = JSON.parse(action.payload.luckySheetConfigStr)
      }
      newState.luckySheetOption = JSON.stringify(JSON.parse(JSON.stringify(defaultOption)))
      newState.viewId = action.payload.id
      newState.insertDate = action.payload.insertDate
      return newState

    // 获取list
    case "change-tempList":
      // 将正在使用的id与正在查看的id传入redux
      action.payload.map((item) => {
        if (item.state === 1) {
          newState.stateId = item.id
        }
        // if (item.isView === 1) {
        //   newState.viewId = item.id
        // }
        return item
      })
      newState.tempList = action.payload
      return newState

    // 删除指定数据
    case "delete-tempList":
      let id = action.payload
      // 此处关键点，两个对象互相赋值只是将指针交换，所以他们的引用是一样的，redux无法判断是否发生改变，进而无法通知页面更新
      let newtempList = JSON.parse(JSON.stringify(prevState.tempList))
      newtempList.map((item, index) => {
        if (item.id === id) {
          newtempList.splice(index, 1)
        }
        return newtempList
      })
      newState.tempList = newtempList
      return newState

    // 修改正在使用的模板id
    case "change-stateId":
      newState.stateId = action.payload
      return newState

    // 备注传值,每次修改输入框中的值都会传入redux中
    case "change-commit":
      newState.saveCommit = action.payload
      return newState

    // 修改更新日期及模板
    case "change-saveData":
      newState.luckySheetOption = action.payload.luckySheetConfigStr
      newState.insertDate = action.payload.updateDate

      return newState


    // 创建新模板后需要更新的东西
    case "change-createTemp":
      newState.viewId = action.payload.viewId
      newState.luckySheetOption = action.payload.luckySheetConfigStr
      newState.insertDate = action.payload.updateDate
      newState.tempList = []
      return newState
    default:
      return prevState
  }
}

export default LineLossTempReducer